package com.chenyi.web.controller.monitor;

import com.chenyi.common.annotation.Log;
import com.chenyi.common.constant.Constants;
import com.chenyi.common.core.controller.BaseController;
import com.chenyi.common.core.domain.model.LoginAdmin;
import com.chenyi.common.core.page.TableDataInfo;
import com.chenyi.common.core.redis.RedisCache;
import com.chenyi.common.enums.BusinessType;
import com.chenyi.common.utils.StringUtils;
import com.chenyi.system.domain.SysAdminOnline;
import com.chenyi.system.service.ISysAdminOnlineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * 在线用户监控
 * 
 * @author wangkh
 */
@RestController
@RequestMapping("/admin/monitor/online")
public class AdminOnlineController extends BaseController
{
    @Autowired
    private ISysAdminOnlineService userOnlineService;

    @Autowired
    private RedisCache redisCache;

    @PreAuthorize("@admin.hasPermi('monitor:online:list')")
    @GetMapping("/list")
    public ResponseEntity<TableDataInfo> list(String ipaddr, String userName)
    {
        Collection<String> keys = redisCache.keys(Constants.ADMIN_LOGIN_TOKEN_KEY + "*");
        List<SysAdminOnline> userOnlineList = new ArrayList<SysAdminOnline>();
        for (String key : keys)
        {
            LoginAdmin user = redisCache.getCacheObject(key);
            if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName))
            {
                if (StringUtils.equals(ipaddr, user.getIpaddr()) && StringUtils.equals(userName, user.getUsername()))
                {
                    userOnlineList.add(userOnlineService.selectOnlineByInfo(ipaddr, userName, user));
                }
            }
            else if (StringUtils.isNotEmpty(ipaddr))
            {
                if (StringUtils.equals(ipaddr, user.getIpaddr()))
                {
                    userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipaddr, user));
                }
            }
            else if (StringUtils.isNotEmpty(userName) && StringUtils.isNotNull(user.getAdmin()))
            {
                if (StringUtils.equals(userName, user.getUsername()))
                {
                    userOnlineList.add(userOnlineService.selectOnlineByUserName(userName, user));
                }
            }
            else
            {
                userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
            }
        }
        Collections.reverse(userOnlineList);
        userOnlineList.removeAll(Collections.singleton(null));
        return new ResponseEntity<>(getDataTable(userOnlineList), HttpStatus.OK);
    }

    /**
     * 强退用户
     */
    @PreAuthorize("@admin.hasPermi('monitor:online:forceLogout')")
    @Log(title = "在线用户", businessType = BusinessType.FORCE)
    @DeleteMapping("/{tokenId}")
    public ResponseEntity<Object> forceLogout(@PathVariable String tokenId)
    {
        redisCache.deleteObject(Constants.ADMIN_LOGIN_TOKEN_KEY + tokenId);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
